//
//  HeartRateAverageChartView.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/17.
//

import UIKit
import MYModule

open class HeartRateChartFillView: View {
    
   public var hearts: [Int]? {
        
        didSet {
            
            guard let hearts = hearts else {
                emptyBarChartView()
                return
            }
            
            let zeroHearts = hearts.filter({$0 > 0 })
            if zeroHearts.isEmpty {
                emptyBarChartView()
                return
            }
            
            let average = Double(hearts.reduce(0, +)) / Double(hearts.count)
            
            barChartView.setConfig(DFLineChartViewConfig(
                xAxisLabelFont: Fonts.systemFont(ofSize: 10),
                xAxisLabelTextColor: .clear,
                xAxisLineColor: .clear,
                xAxisLineHeight: 0,
                leftAxisLimit: true,
                limit: average
            ))
            
            barChartView.setData( hearts.map { Double($0) },
                                  lineColor: UIColor.HeartRate.theme,
                                  gradientColors: UIColor.HeartRate.gradient,
                                  lineWidth: 1)
            
        }
       
   }
    
    open override func makeUI() {
        super.makeUI()
        

        self.addSubview(titleLabel)
        self.addSubview(hrvStackView)
        self.addSubview(avgHrLabel)
        self.addSubview(maxOrMinStackView)
        self.addSubview(barChartView)
        
    
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(15)
            make.leading.equalToSuperview().offset(20)
            make.trailing.equalToSuperview().offset(-20)
        }
        
        hrvStackView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(15)
            make.centerX.equalToSuperview()
        }
        
        avgHrLabel.snp.makeConstraints { make in
            make.top.equalTo(hrvStackView.snp.bottom)
            make.centerX.equalToSuperview()
        }
        
        maxOrMinStackView.snp.makeConstraints { make in
            make.top.equalTo(avgHrLabel.snp.bottom).offset(10)
            make.leading.trailing.equalToSuperview()
        }
        
        barChartView.snp.makeConstraints { make in
            make.top.equalTo(maxOrMinStackView.snp.bottom).offset(18)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(150)
            make.bottom.equalToSuperview()
        }
    
    }

    public lazy var titleLabel: Label = {
        let titleLabel = Label()
        titleLabel.text = "sleep_average_heart_rate".language
        titleLabel.font = Fonts.boldSystemFont(ofSize: 14)
        titleLabel.textColor = UIColor.assist_222222
        return titleLabel
    }()
    
    private lazy var hrvStackView: StackView = {
        let hrvStackView = StackView()
        hrvStackView.alignment = .firstBaseline
        hrvStackView.spacing = 2
        hrvStackView.axis = .horizontal
        hrvStackView.distribution = .fill
        hrvStackView.addArrangedSubview(hrvLabel)
        hrvStackView.addArrangedSubview(unitLabel)
        return hrvStackView
    }()
    
    public lazy var hrvLabel: Label = {
        let hrvLabel = Label()
        hrvLabel.text = "--"
        hrvLabel.textColor = UIColor.assist_222222
        hrvLabel.font = Fonts.boldSystemFont(ofSize: 44)
        return hrvLabel
    }()

    public lazy var unitLabel: Label = {
        let unitLabel = Label()
        unitLabel.text = "unit_hrv".language
        unitLabel.font = Fonts.systemFont(ofSize: 12)
        unitLabel.textColor = UIColor.assist_222222
        return unitLabel
    }()
    
    public lazy var avgHrLabel: Label = {
        let avgHrLabel = Label()
        avgHrLabel.text = "sleep_avg_hr".language
        avgHrLabel.font = Fonts.systemFont(ofSize: 12)
        avgHrLabel.textColor = UIColor.assist_222222
        return avgHrLabel
    }()
    
    
    public lazy var maxOrMinStackView: StackView = {
        let maxOrMinStackView = StackView()
        maxOrMinStackView.alignment = .center
        maxOrMinStackView.axis = .horizontal
        maxOrMinStackView.distribution = .fillEqually
        maxOrMinStackView.addArrangedSubview(maxHRVView)
        maxOrMinStackView.addArrangedSubview(minHRVView)
        return maxOrMinStackView
    }()
    
    public lazy var maxHRVView: HeartRateSubDataView = {
        let maxHRVView = HeartRateSubDataView()
        maxHRVView.setConfig(icon: "ic_sleep_max",
                             hrv: "--",
                             until: "unit_hrv".language,
                             textColor: UIColor.assist_222222,
                             desc: "sleep_max_hrv".language)
        return maxHRVView
    }()
    
    public lazy var minHRVView: HeartRateSubDataView = {
        let minHRVView = HeartRateSubDataView()
        minHRVView.setConfig(icon: "ic_sleep_min",
                             hrv: "--",
                             until: "unit_hrv".language,
                             textColor: UIColor.assist_222222,
                             desc: "sleep_min_hrv".language)
        return minHRVView
    }()
    
    public lazy var barChartView: DFLineChartView = {
        let barChartView = DFLineChartView.init(frame:.zero)
        
        return barChartView
    }()
    
    

    
    /// 缺省显示图表
   public func emptyBarChartView() {
        
       let lineColor = UIColor.assist_D0D0D0
       let colors = UIColor.HeartRate.emptyGradient
        
        let waveRate: Double = 0.01
        let waveSpeed: Double = 0.05
        let waveHeight: Double = 20
        var x: Double = 0.1
        
        var offset: Double = 0
        let all = (0 ..< 4).map { i -> [Double] in
            offset += waveSpeed
            let y = waveHeight * sin(x * waveRate + offset)
            let realY = y + 12 + Double(i)
            let maskY = -y + 12 + Double(i)
            x += 0.1
            
            return [maskY, realY]
        }
        let emptyData = all.flatMap { $0 }
        
//        if let max = emptyData.max() {
//            barChartView.setYMax(value: max)
       //        }
       
       // 缺省图不需要中间的线
       barChartView.setConfig(DFLineChartViewConfig(
        xAxisLabelFont: Fonts.systemFont(ofSize: 10),
        xAxisLabelTextColor: .clear,
        xAxisLineColor: .clear,
        xAxisLineHeight: 0,
        leftAxisLimit: false,
        limit: 0
       ))
       
       barChartView.setData(emptyData,
                            lineColor: lineColor.withAlphaComponent(0.3),
                            gradientColors: colors,
                            lineWidth: 1)
       
   }
    
    

}
